package com.zues.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体主题（天气数据中心）
 */
public class WeatherDataCenter implements WeatherSubject{

    private double temperature;
    private final List<WeatherObserver> observers = new ArrayList<>();


    @Override
    public void addObserver(WeatherObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(WeatherObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (WeatherObserver observer : observers) {
            observer.update(temperature);
        }
    }

    @Override
    public void setTemperature(double temperature) {
        this.temperature = temperature;
        notifyObservers(); //温度变化时主动通知
    }
}
